package com.ruoyi.fs.service.impl;

import java.util.List;

import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.fs.domain.FsCaseRepaymentSchedule;
import com.ruoyi.fs.domain.FsCaseRepaymentSchedulePay;
import com.ruoyi.fs.mapper.FsCaseRepaymentScheduleMapper;
import com.ruoyi.fs.mapper.FsCaseRepaymentSchedulePayMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.fs.mapper.FsCaseRepaymentMapper;
import com.ruoyi.fs.domain.FsCaseRepayment;
import com.ruoyi.fs.service.IFsCaseRepaymentService;

/**
 * 案件还款Service业务层处理
 *
 * @author hero
 * @date 2024-11-11
 */
@Service
public class FsCaseRepaymentServiceImpl implements IFsCaseRepaymentService {
    @Autowired
    private FsCaseRepaymentMapper fsCaseRepaymentMapper;
    @Autowired
    private FsCaseRepaymentScheduleMapper fsCaseRepaymentScheduleMapper;
    @Autowired
    private FsCaseRepaymentSchedulePayMapper fsCaseRepaymentSchedulePayMapper;

    /**
     * 查询案件还款
     *
     * @param id 案件还款主键
     * @return 案件还款
     */
    @Override
    public FsCaseRepayment selectFsCaseRepaymentById(Long id) {
        return fsCaseRepaymentMapper.selectFsCaseRepaymentById(id);
    }

    /**
     * 查询案件还款列表
     *
     * @param fsCaseRepayment 案件还款
     * @return 案件还款
     */
    @Override
    public List<FsCaseRepayment> selectFsCaseRepaymentList(FsCaseRepayment fsCaseRepayment) {
        return fsCaseRepaymentMapper.selectFsCaseRepaymentList(fsCaseRepayment);
    }

    /**
     * 新增案件还款
     *
     * @param fsCaseRepayment 案件还款
     * @return 结果
     */
    @Override
    public int insertFsCaseRepayment(FsCaseRepayment fsCaseRepayment) {
        fsCaseRepayment.setCreateTime(DateUtils.getNowDate());
        return fsCaseRepaymentMapper.insertFsCaseRepayment(fsCaseRepayment);
    }

    /**
     * 修改案件还款
     *
     * @param fsCaseRepayment 案件还款
     * @return 结果
     */
    @Override
    public int updateFsCaseRepayment(FsCaseRepayment fsCaseRepayment) {
        fsCaseRepayment.setUpdateTime(DateUtils.getNowDate());
        return fsCaseRepaymentMapper.updateFsCaseRepayment(fsCaseRepayment);
    }

    /**
     * 批量删除案件还款
     *
     * @param ids 需要删除的案件还款主键
     * @return 结果
     */
    @Override
    public int deleteFsCaseRepaymentByIds(Long[] ids) {
        return fsCaseRepaymentMapper.deleteFsCaseRepaymentByIds(ids);
    }

    /**
     * 删除案件还款信息
     *
     * @param id 案件还款主键
     * @return 结果
     */
    @Override
    public int deleteFsCaseRepaymentById(Long id) {
        return fsCaseRepaymentMapper.deleteFsCaseRepaymentById(id);
    }

    @Override
    public FsCaseRepayment getCaseRepaymentDetailByCaseId(Long caseId) {
        FsCaseRepayment query = new FsCaseRepayment();
        query.setCaseId(caseId);
        List<FsCaseRepayment> list = fsCaseRepaymentMapper.selectFsCaseRepaymentList(query);
        if (list.isEmpty()) {
            return null;
        } else {
            FsCaseRepayment repayment = list.get(0);
            FsCaseRepaymentSchedule schedule = new FsCaseRepaymentSchedule();
            schedule.setRepaymentId(repayment.getId());
            List<FsCaseRepaymentSchedule> schedules = fsCaseRepaymentScheduleMapper.selectFsCaseRepaymentScheduleList(schedule);
            if (!schedules.isEmpty()) {
                for (int i = 0; i < schedules.size(); i++) {
                    FsCaseRepaymentSchedulePay pay = new FsCaseRepaymentSchedulePay();
                    pay.setScheduleId(schedules.get(i).getId());
                    List<FsCaseRepaymentSchedulePay> pays = fsCaseRepaymentSchedulePayMapper.selectFsCaseRepaymentSchedulePayList(pay);
                    schedules.get(i).setPays(pays);
                }
                repayment.setHkjhList(schedules);
            }
            return repayment;
        }
    }
}
